package com.kiloai.base.custom;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kiloai.base.R;
import com.kiloai.base.utils.PixelUtil;


/**
 * Created by david on 17/6/9.
 */

public class CompatTitleBar extends RelativeLayout {

    private final String TAG = "CompatTitleBar";
    /**
     * 左侧按钮ID
     */
    public static final int ID_RETURN = 0;

    /**
     * 右侧按钮ID
     */
    public static final int ID_RIGHT = 1;

    /**
     * 右侧ICON ID
     */
    public static final int ID_ICON_RIGHT = 2;

    /**
     * 按钮监听器
     *
     * @param context
     */
    private OnTitleBarClickListener mListener;

    private View mBottom;
    private ImageView mIconLeft;
    private TextView mTitle;
    private TextView mTitleRight;

    private ImageView mIconRight;
    private LinearLayout ll_root;
    private FrameLayout paddingLayout;


    public CompatTitleBar(Context context) {
        super(context);
    }

    public CompatTitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.layout_compat_title, this);
        initView();
        addPaddinView();
        setListeners();
    }


    private void initView() {
        mBottom = findViewById(R.id.view_bottomline);
        paddingLayout = (FrameLayout) findViewById(R.id.fl_padding);
        mIconLeft = (ImageView) findViewById(R.id.iv_return);
        mTitle = (TextView) findViewById(R.id.tv_title);
        mTitleRight = (TextView) findViewById(R.id.tv_title_right);
        mIconRight = (ImageView) findViewById(R.id.iv_icon_right);
        ll_root = (LinearLayout) findViewById(R.id.ll_root);
    }

    // android 4.4以上将Toolbar添加状态栏高度的上边距，沉浸到状态栏下方
    private void addPaddinView() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            View view = new View(getContext());
            LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, PixelUtil.getStatusHeight(getContext()));
            view.setLayoutParams(params);
            paddingLayout.addView(view);
        }
    }

    /**
     * 设置中间按钮文字
     *
     * @param title
     */
    public void setCenterTitle(String title) {
        if (mTitle != null) {
            mTitle.setText(title);
        }
    }

    /**
     * 设置右边按钮文字
     *
     * @param title
     */
    public void setRightTitle(String title) {
        if (mTitleRight != null) {
            if (title.equals("")) {
                mTitleRight.setVisibility(GONE);
            } else {
                mTitleRight.setVisibility(VISIBLE);
                mTitleRight.setText(title);
            }
        }
    }

    /**
     * 设置右边的icon
     *
     * @param resId icon.id
     */
    public void setRightIcon(int resId) {
        if (mIconRight != null) {
            mIconRight.setImageResource(resId);
            mIconRight.setVisibility(VISIBLE);
        }
    }

    /**
     * 设置右边icon隐藏
     */
    public void setRightIconInvisiable() {
        if (mIconRight != null) {
            mIconRight.setVisibility(INVISIBLE);
        }
    }

    /**
     * 设置title 背景颜色
     *
     * @param color R.color.xx
     */
    public void setTitleBarBg(int color) {
        if (ll_root != null) {
            ll_root.setBackgroundResource(color);
        }
    }

    /**
     * 设置bottom line 背景颜色
     * 传入
     *
     * @param color R.color.xx
     */
    public void setBottomBg(int color) {
        if (mBottom != null) {
            if (color == 0) {
                mBottom.setVisibility(GONE);
            } else
                mBottom.setBackgroundResource(color);
        }
    }

    /**
     * 设置左边边的icon
     * 传入0为隐藏该按钮
     *
     * @param resId icon.id
     */
    public void setLeftIcon(int resId) {
        if (mIconLeft != null) {
            if (resId == 0) {
                mIconLeft.setVisibility(GONE);
            } else {
                mIconLeft.setVisibility(VISIBLE);
                mIconLeft.setImageResource(resId);
            }
        }
    }

    public void setRightTitleEnable(boolean enable) {
        mTitleRight.setEnabled(enable);
        mTitleRight.setClickable(enable);
    }


    private void setListeners() {

        // icon_return  直接执行finish操作
        mIconLeft.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onCommonTitleClick(ID_RETURN);
                }

            }
        });

        mTitleRight.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onCommonTitleClick(ID_RIGHT);
                }

            }
        });

        mIconRight.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    mListener.onCommonTitleClick(ID_ICON_RIGHT);
                }
            }
        });
    }


    public void setOnTitleBarClickListener(OnTitleBarClickListener listener) {
        mListener = listener;
    }

    public interface OnTitleBarClickListener {
        public void onCommonTitleClick(int id);
    }

}
